%!TEX root = report.tex

\section{Technologies}
\label{sec:technologies}

In this section we provide some background information about the tools we studied in the project, and how these tools fit together.

\subsection{Xtext}

The first milestone in the project was to create a domain specific language, which can be used for specifying modes in a bicycle computer. 
On one hand, having experience in writing programs we wanted to make it look like a real programming language. 
On the other hand, we had to take into account that the end user of a particular DSL is likely to be a domain expert, not a software developer, and thus he or she might have limited knowledge about how to write programs. 
Because of that, we decided to create a textual DSL with a strict structure, which is easy to use.

To address our decision we chose a tool called Xtext. 
Xtext is an open source framework for development of textual DSLs. 
The first version of Xtext was published in 2006, and since that time, it has been in active development. 
The tool is created for being used inside the Eclipse IDE and relies on the Eclipse Modeling framework (EMF)~\cite{www:emf}.

To create a domain specific language using Xtext, the only one task that a developer has to complete is to write the grammar of the future DSL. 
Xtext uses Extended BNF for describing grammars; these grammars are LL(1) grammars. 
The tool also provides a convenient editor for writing grammars. 
Once the developer completed the grammar, Xtext will create a plug-in for Eclipse that contains all necessary parts, such as a special text editor, parser, and AST meta-model. 
All these parts are created based on the grammar specified before. 
If there is a need for altering an existed DSL, the developer might address it in a limited time by doing changes to the grammar.

One of the key features of the compiled plug-in is the text editor; end users (e.g., domain experts) use it as a main tool and are not required to think about the processes that run behind it. 
The editor has many good features such as Syntax coloring, Code completion, Validation and more. 
The editor provides coloring for comments, keywords, numbers, and string literals. 
If users do not like the default font schema, they can adjust it to meet their needs. 
Figure~\ref{fig:xtext-coloring} contains an example of syntax coloring. 
The editor also helps end users with correct using of the DSL by supporting Code completion feature (Fig.~\ref{fig:xtext-completion}).
When a user calls Code completion, it shows a list of correct statements for a particular place in the file. Validation feature is another good feature that helps end users. 
Xtext constantly checks the input not only for syntax errors, but also looks for any unresolved references. 
If it finds such an error, it underlines the place in the file that causes the problem and reports the error into a standard Error View window. 

\begin{figure}
	\begin{center}
	\includegraphics[scale=0.7]{imgs/feature2_syntax_coloring.png}
	\end{center}
	\caption{Syntax coloring in Xtext editor}
	\label{fig:xtext-coloring}
\end{figure} 

\begin{figure}
	\begin{center}
	\includegraphics[scale=0.7]{imgs/feature2_content_assist.png}
	\end{center}
	\caption{Code completion in Xtext editor}
	\label{fig:xtext-completion}
\end{figure} 

\subsection{Xtend}

For the second milestone in our project, creating a code generator, we chose Xtend. 
Xtend is a statically-typed template language with a Java-like syntax that compiles into Java code and is used for creating code generators. 
Because Xtend tightly bundled with the Xtext tool (it comes with Xtext SDK) we decided that we would be able to create a better code generator for a bicycle computer using it rather than any other tool.
The key feature of Xtend is string templates (Fig.~\ref{fig:xtend-template}) that are used for creating code generators. 
Xtend comes with a special editor for Eclipse that provides a convenient way for writing string templates and helps to keep Xtend program readable.
In addition, because code generation is based on templates, it preserve all whitespacing and identation in generated code.
Although Xtend compiles into Java, it can be used for creating code generators for any programming language. 

To create a transformation tool that can generate a source code from the program written in a domain specific language, using Xtend one has to (a) write string templates for future code, (b) define iterations over AST, and (c) implement any additional helper functions. 
When it is done, the program compiles into Java code and is ready for execution. 


\begin{figure}
	\begin{center}
	\includegraphics[scale=0.7]{imgs/feature2_template_expressions.png}
	\end{center}
	\caption{A code snipet of string template}
	\label{fig:xtend-template}
\end{figure} 

\subsection{Integration}

Figure~\ref{fig:data-flow} demonstrates a high-level picture of how all components work together in our project. 
Green background color refers to the steps where a person plays a key role; blue background color refers to external tools; red background color refers to generated or compiled code.

In the beginning, the developer creates the grammar for a domain specific language that is consumed by Xtext. 
This grammar defines a meta-model for a future DSL. 
Then the same person creates the code templates using Xtend. 
At this place an implicit link between Xtext and Xtend appears: Xtend uses the output from Xtext (AST meta model) to define injections into templates.
Finally, all this compiles into an Eclipse plug-in.

At the next stage another person (e.g., a domain expert) uses this plug-in to define modes for a bicycle computer. 
He writes them using a little language created during the previous stage. 
One of the features of Xtend is that it generates code (Java code in our example) from a model on the fly as long as the person provides a valid input.

At the final stage, the same person takes the generated code and put it into the framework. 
After that, a complete bicycle computer program is compiled from these sources. 


\begin{figure}
	\includegraphics[width=1.0\columnwidth]{imgs/data_flow.png}
	\caption{Data flow in the project}
	\label{fig:data-flow}
\end{figure} 
